package com.gqz.aircraftwar.mvp.launch

import android.os.Bundle
import androidx.databinding.BaseObservable
import androidx.databinding.ObservableField
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.gqz.aircraftwar.databinding.ActLaunchBinding
import com.gqz.aircraftwar.util.Preference
import com.gqz.aircraftwar.util.i

/**
 * Descraption
 *
 * @author gqz
 * @data 2020/3/21
 */
class LaunchPresenter(view: LaunchConstract.IView) :
    LaunchConstract.IPresetner, BaseObservable() {

    private val TAG: String = "LaunchPresenter-->"

    private lateinit var binding: ActLaunchBinding
    var playerName: ObservableField<String> = ObservableField("Player1")
    var view: LaunchConstract.IView? = null
    lateinit var sp: Preference

    init {
        i(TAG, "通过主构造函数初始化类")
        this.view = view
        view.setIPresenter(this)
    }

    override fun <T> setBinding(binding: T) {
        this.binding = binding as ActLaunchBinding
        binding.name = playerName
    }

    override fun onResume(owner: LifecycleOwner) {
        i(TAG, "onResume-->")
    }

    override fun onPause(owner: LifecycleOwner) {
        i(TAG, "onPause-->")
    }

    override fun onLifecycleChanged(owner: LifecycleOwner, event: Lifecycle.Event) {
        i(
            TAG, "onLifecycleChanged--> event=${event.name}"
        )
    }

    override fun loadPlayerInfo() {
        sp = Preference(playerName.get() as String)
        sp.put("name", binding.name?.get())
        val bundle = Bundle()
        var player: String? = binding.name?.get()
        bundle.putString("player", player ?: "")
        view?.enterMain(bundle)
    }
}
